##Setup scripts with apkn customizations
##implements functions listed below
#1. Initializes a Z Pyscopg database connection (Should already be 
#installed)
#2. Manages property settings for the SQL pas plugins - source_roles,
# source_users and source_groups
#3. Disable search for users from within the Plone site
#4. Additional group management - and roles setup
#5. 
##

from Products.CMFPlone.utils import _createObjectByType
from Products.CMFCore.utils import getToolByName
from urllib import unquote
#import configuration settings - look at config.py for details
from apkn.policy.config import *
from Products.CMFPlone import PloneMessageFactory as _

def setupVarious(context):
	return


def setupPropertySet(target,propertySet):
	for param_name,param_value in propertySet.items():
		expr = "target.manage_changeProperties(%s=\"%s\")" \
		%(param_name, unquote(param_value))			
		eval(expr)	
	return


def configureAuthSettings(context):
	return


def setupSessionPluginProperties(context):
	portal = context.getSite()
	sess_plugin = None
	if hasattr(portal, 'acl_users'):
		pas_obj = getattr(portal,'acl_users')
		if hasattr(pas_obj, 'session'):
			sess_plugin = getattr(pas_obj, 'session')
	if sess_plugin:
		try:
			sess_plugin.manage_changeProperties(SESSION_PLUGIN_PROPERTIES)
		except:
			portal.plone_log("Failed to set up session plugin properties")
			
		#now set up the shared secret across both applications
		try:
			sess_plugin._shared_secret = SESSION_SHARED_SECRET
		except:
			portal.plone_log("Failed to set up shared session secret")
	else:
		portal.plone_log("Session plugin does not exist in this Plone")
	
	
def createRootFolders(context):
	'''
	Create the root folder in the main
	site folder if they do not exist
	These folders represent the subsites
	'''
	
	portal = context.getSite()
	objectIdentities = portal.objectIds()
	try:
		for folder in ROOT_FOLDER_LIST.split(","):
			if folder not in objectIdentities:
				portal.manage_addFolder(id=folder, title=folder.upper())
			else:
				portal.plone_log("Failed to add folder.\
				 A folder with the same name already exists")
	except:
		pass
	return

def setupLanguage(context):
	'''
	Set up the default Plone language settings.
	See config.py to change these to suit your
	setup
	'''
	portal = context.getSite()
	lang_tool = portal.portal_languages
	lang_tool.manage_setLanguageSettings(
		defaultLanguage = DEFAULT_LANGUAGE,
		supportedLanguages = SUPPORTED_LANGUAGES,
		displayFlags = DISPLAY_FLAGS,
		setForcelanguageUrls = 0,
		setCookieN = 1,
		setContentN = 0,
		setRequestN = 1
	)
	portal.plone_log("Finished setting up language properties for Plone")
	return
